package com.feng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {
    @ResponseBody()
    @RequestMapping("/name")
    public String user(String name){
        System.out.println("name==>"+name);
        if (!name.equals("root")){
            return "账号错误";
        }
        return  "账号正确";
    }


    @RequestMapping("/login")
    public String login(HttpSession session, HttpServletRequest request, HttpServletResponse response, String username) throws ServletException, IOException {
        System.out.println("username==>"+username);
        if (!username.equals("root")){
            return "forward:/user/failLogin";
        }
        session.setAttribute("username",username);
//        request.getRequestDispatcher("/user/book").forward(request,response);
        return  "forward:/user/book";
    }

    @ResponseBody
    @RequestMapping("failLogin")
    public  String fail(){
        System.out.println("==================账号或密码错误");
        return "账号或密码错误!";
    }

    //登陆成功
    @ResponseBody
    @RequestMapping("/book")
    public String book(){
        return "success";
    }
    //注销session
    @RequestMapping("/remove")
    public String move(HttpSession session){
        session.removeAttribute("username");
        return "forward:/index.jsp";
    }
}
